package com.woniuxy.lab.woniu_lab_boss.docker;


import com.woniuxy.lab.woniu_lab_boss.common.Shell;

public class Run {

    private Shell shell;

    private final static String DOCKER_RUN = "docker run ";

    private final static String SPACE = " ";

    public Run() {
    }

    public Run(Shell shell) {
        this.shell = shell;
    }

    public static Run getRun(Shell shell) {
        return new Run(shell);
    }

    // --------------

    public String runParam(String name) {
        String param = "-d --rm ";
        param = param + "--name " + name;
        return param;
    }

    public String runParam(String h, String name) {
        String param = "-d --rm ";
        param = param + "-h " + h + "--name " + name;
        return param;
    }

    public String runParam(String h, String name, String pOuter, String pInner) {
        String param = "-d --rm ";
        param = param + "-h " + h + " --name " + name + " -p " + pOuter + ":" + pInner;
        return param;
    }

    public String runParam(String h, String name, String pOuter, String pInner, String network) {
        String param = "-d --rm ";
        param = param + "-h " + h + " --name " + name + " -p " + pOuter + ":" + pInner + " --network " + network;
        return param;
    }

    public String runParam(String h, String name, String pOuter, String pInner, String vOuter, String vInner, String network) {
        String param = "-d --rm ";
        param = param + "-h " + h + " --name " + name + " -p " + pOuter + ":" + pInner + " -v " + vOuter + ":" + vInner + " --network " + network;
        return param;
    }

    public String runParam(Boolean d, Boolean rm, String h, String name, String pOuter, String pInner, String vOuter, String vInner, String network) {
        String param = "";
        if (d) param += "-d ";
        if (rm) param += "--rm ";
        param = param + "-h " + h + " --name " + name + " -p " + pOuter + ":" + pInner + " -v " + vOuter + ":" + vInner + " --network " + network;
        return param;
    }

    public String runParam(Boolean d, Boolean rm, String e, String h, String name, String pOuter, String pInner, String vOuter, String vInner, String network) {
        String param = "";
        if (d) param += "-d ";
        if (rm) param += "--rm ";
        param = param + "-e "+e;
        param = param + " -h " + h + " --name " + name + " -p " + pOuter + ":" + pInner + " -v " + vOuter + ":" + vInner + " --network " + network;
        return param;
    }

    public String run(String runParam, String nameOrId){
        return shell.execCommand(DOCKER_RUN+runParam+SPACE+nameOrId);
    }

    /*
        该命令属于docker的容器命令，基本格式如下：
        docker [container] run [参数] run [镜像名]
        其中container通常省略不写，该命令相当于合成了pull、create和start三个命令。
        常用参数：

        -d 指定该容器后台运行
        --rm 设置为一次性容器，在容器关闭时自动删除
        -it 为容器分配一个TTY终端，且容器启动后进入命令行。
        -e [...] 设置容器的环境变量，其后更设置的字段
        -h [...] 指定该容器在网络中的主机名
        --name [...] 指定该容器名称
        -p [outer:inner] 端口映射，外部端口写在前
        -v [outer:inner] 卷映射
        --restart=always 设置该容器始终跟随docker重启
        --network=[网络名] 为该容器指定网络
     */


}
